package cn.gzjp.modules.logis.service;

import java.math.BigDecimal;
import java.util.List;

import cn.gzjp.modules.gas.entity.GasUser;
import cn.gzjp.modules.logis.entity.LogisCar;
import cn.gzjp.modules.logis.entity.LogisUserUnion;
import cn.gzjp.modules.wx.entity.WxMpAccount;
import cn.gzjp.modules.wx.service.WxMpAccountService;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.service.CrudService;
import cn.gzjp.modules.logis.entity.LogisUser;
import cn.gzjp.modules.logis.dao.LogisUserDao;

/**
 * 物流用户Service
 * @author zengcp
 * @version 2018-03-21
 */
@Service
@Transactional(readOnly = true)
public class LogisUserService extends CrudService<LogisUserDao, LogisUser> {

	@Autowired
	private LogisUserUnionService logisUserUnionService;
	@Autowired
	private WxMpAccountService wxMpAccountService;
	@Autowired
	private LogisCarService logisCarService;

	public LogisUser get(String id) {
		LogisUser user =  super.get(id);
		LogisCar logisCar = logisCarService.get(id);
		if(logisCar!=null){//确认司机是否认证
			user.setCarStatus(logisCar.getCarStatus());
			user.setDriverStatus(logisCar.getDriverStatus());
		}
		return user;
	}
	
	public List<LogisUser> findList(LogisUser logisUser) {
		return super.findList(logisUser);
	}
	
	public Page<LogisUser> findPage(Page<LogisUser> page, LogisUser logisUser) {
		return super.findPage(page, logisUser);
	}
	

	@Transactional(readOnly = false)
	public void save(LogisUser user) {
		LogisUser m = get(user.getId());
		WxMpAccount mpAccount = wxMpAccountService.get(user.getAppId());//加上类型
		if(StrUtil.isNotBlank(user.getUnionId())&&!user.getUnionId().endsWith("_"+mpAccount.getType())){
			user.setUnionId(user.getUnionId()+"_"+mpAccount.getType());
		}
		if(m!=null){
			super.save(user);
		}else{
			super.insert(user, user.getId());
		}
		if(StrUtil.isNotBlank(user.getUnionId())){
			LogisUserUnion unionUser = logisUserUnionService.get(user.getUnionId());
			if(unionUser!=null) {

				unionUser.setId(user.getUnionId());
				unionUser.setMobile(user.getMobile());
				unionUser.setHeadImg(user.getHeadImg());
				unionUser.setNickName(user.getNickName());
				if(StrUtil.isNotBlank(user.getMobile())){
					unionUser.setMobile(user.getMobile());
				}
				unionUser.setRealName(user.getRealName());
				user = this.get(user.getId());
				if(user.getBalance()!=null){
					unionUser.setBalance(user.getBalance());
				}
				unionUser.setScoreTotal(user.getScoreTotal());
				unionUser.setShouhuoNum(user.getShouhuoNum());
				unionUser.setJiaocheNum(user.getJiaocheNum());
				unionUser.setJiedanNum(user.getJiedanNum());
				if(user.getCreditTotal()>0){
					unionUser.setCreditTotal(user.getCreditTotal());//默认100信用积分
				}
				if(user.getIncomeTotal()!=null){
					unionUser.setIncomeTotal(user.getIncomeTotal());
				}
				if(user.getCommissionTotal()!=null){
					unionUser.setCommissionTotal(user.getCommissionTotal());
				}
				unionUser.setFleetNum(user.getFleetNum());
				unionUser.setFriendNum(user.getFriendNum());
				logisUserUnionService.save(unionUser);//更新统一用户
			}else{
				unionUser = new LogisUserUnion();
				unionUser.setId(user.getUnionId());
				unionUser.setHeadImg(user.getHeadImg());
				unionUser.setNickName(user.getNickName());
				unionUser.setMobile(user.getMobile());
				unionUser.setRealName(user.getRealName());
				unionUser.setBalance(new BigDecimal("0"));
				unionUser.setScoreTotal(0);
				unionUser.setShouhuoNum(0);
				unionUser.setJiaocheNum(0);
				unionUser.setJiedanNum(0);
				unionUser.setScoreTotal(0);//默认0积分
				unionUser.setCreditTotal(100);//默认100信用积分
				unionUser.setIncomeTotal(new BigDecimal(0));
				unionUser.setCommissionTotal(new BigDecimal(0));
				unionUser.setFleetNum(0);
				unionUser.setFriendNum(0);
				logisUserUnionService.save(unionUser);//新增统一用户
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(LogisUser logisUser) {
		super.deleteByLogic(logisUser);
	}
	
}